layui.use([ 'form'], function() {
    var form = layui.form;
    let $ = layui.$;
    // 加载角色列表，渲染角色复选框
    initRoleList();
    //监听提交
    form.on('submit(formDemo)', function(data){
        if(data.field.newPassword != data.field.confirmPassword) {
            layer.msg('密码不一致')
            return false
        }
        if(data.field.newPassword == data.field.oldPassword) {
            layer.msg('新密码和旧密码不能相同')
            return false
        }
        $.ajax({
            dataType: 'json',
            type: 'PUT',
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify(data.field),
            url: Api.uaa.person.changePassword,
            success: function (result, status, xhr) {
                if(result.code == 200) {
                    window.location = 'index.html'
                    layer.msg(result.msg)
                } else {
                    layer.alert(result.msg)
                }
            },
            error: function(xhr, status, error) {
                // layer.alert(error)
                if(xhr.responseJSON) {
                    layer.alert(xhr.responseJSON.msg)
                }
            }
        })
        return false;
    });

    function initRoleList() {
        $.getJSON(Api.uaa.role, {page:1, limit:100}, function (res) {
            for(let role of res.data.records) {
                $("#roleCheckbox").append("<div class=\"layui-input-inline\" style='width: 120px;'><input type=\"checkbox\" name=\"roleId\" value='"+role.roleId+"' title=\""+role.roleName+"\"></div>");
            }
            form.render('checkbox')
        })
    }
})
